package com.rfid.service;

import com.google.gson.Gson;
import com.rfid.entity.Book;
import com.rfid.entity.BookForm;
import com.rfid.entity.ExceptBook;
import com.rfid.mapper.BookMapper;
import com.rfid.mapper.ExceptBookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Editor: hengBao
 * Wechat：zh17530588817
 * date: 2022/12/11/16:01
 * title：
 */
@Service
public class RfidService {
    Gson gson = new Gson();

    @Autowired
    private ExceptBookMapper exceptBookMapper;

    @Autowired
    private BookMapper bookMapper;

    @Transactional
    public List<Book> judgeBooks(String bookFromStr) {
        BookForm form = toFrom(bookFromStr);
        List<Book> books = form.getBooks();
        ArrayList<Book> res = new ArrayList<>();
        if (!Objects.isNull(books)) {
            for (Book book : books) {
                Book afterJudgeBook = judgeBook(book);
                if (!Objects.isNull(afterJudgeBook)) {
                    res.add(afterJudgeBook);
                }
            }
        }
        return res;
    }
    @Transactional
    public List<ExceptBook> getExceptBooks() {
        return exceptBookMapper.toGetExceptBooks();
    }

    @Transactional
    public void addExceptBook(String bookFromStr) {
        BookForm form = toFrom(bookFromStr);
        List<ExceptBook> exceptBooks = form.getExceptBooks();
        if (!Objects.isNull(exceptBooks)){
            for (ExceptBook exceptBook : exceptBooks) {
                ExceptBook exceptBook1 = exceptBookMapper.toQueryByRFID(exceptBook.getBookRFID());
                if (!Objects.isNull(exceptBook1)) {
                    exceptBookMapper.toDeleteByRFID(exceptBook.getBookRFID());
                }
                exceptBookMapper.insertExceptBook(exceptBook.getBookRFID(),
                        exceptBook.getBookName(),
                        exceptBook.getCurrentTime(),
                        exceptBook.getCurrentLocation(),
                        exceptBook.getRightLocation());
            }
        }
    }

    private BookForm toFrom(String bookFromStr) {
        return gson.fromJson(bookFromStr, BookForm.class);
    }

    private Book judgeBook(Book beforeBook) {
        String GCBSF = bookMapper.getGCBSFByRFID(beforeBook.getBookRFID());
        if (!Objects.isNull(GCBSF)) {
            String CJBSF = bookMapper.getCJBSFByGCBSF(GCBSF);
            if (!Objects.isNull(CJBSF)) {
                String location = bookMapper.getLocationByCJBSF(CJBSF);
                if (!Objects.isNull(location)) {

                    System.out.println("\n\nlocation:" + location + "\n\n");


                    Book book = new Book();

                    book.setBookRFID(beforeBook.getBookRFID());
                    book.setBookName(bookMapper.getNameByRFID(beforeBook.getBookRFID()));


                    char[] chars = location.toCharArray();
                    StringBuilder builder = new StringBuilder();
                    for (int i = chars.length - 1; i >= 0; i--) {
                        if (chars[i] == '层') {
                        }
                        if (chars[i] == '列') {
                            int j = i - 1;
                            while (chars[j] >= '0' && chars[j] <= '9') {
                                builder.append(chars[j]);
                                j--;
                            }
                            book.setSeries(builder.toString());
                            builder.replace(0, builder.length(), "");
                        }
                        if (chars[i] == '面') {
                            int j = i - 1;
                            while (chars[j] >= 'A' && chars[j] <= 'Z') {
                                builder.append(chars[j]);
                                j--;
                            }
                            book.setFace(builder.toString());
                            builder.replace(0, builder.length(), "");
                        }
                        if (chars[i] == '排') {
                            int j = i - 1;
                            while (chars[j] >= '0' && chars[j] <= '9') {
                                builder.append(chars[j]);
                                j--;
                            }
                            book.setRow(builder.toString());
                            builder.replace(0, builder.length(), "");
                        }
                        if (chars[i] == '室') {
                            book.setBookShelf(location.substring(0, i + 1));
                            builder.replace(0, builder.length(), "");
                        }
                        if (chars[i] == '库') {
                            book.setStoreHouse(location.substring(0, i + 1));
                            builder.replace(0, builder.length(), "");
                        }

                    }
                    book.setIsOK(false);

                    checkBook(beforeBook, book);


                    return book;
                }
            }
        }
        return null;
    }

    private void checkBook(Book beforeBook, Book book) {
        boolean storeHouseFlag, rowFlag, shelfFlag, faceFlag, seriesFlag;
        storeHouseFlag = rowFlag = shelfFlag = faceFlag = seriesFlag = false;
        if (beforeBook.getStoreHouse() != null && beforeBook.getStoreHouse().equals(book.getStoreHouse())) {
            storeHouseFlag = true;

        }
        if (beforeBook.getRow() != null && beforeBook.getRow().equals(book.getRow())) {
            rowFlag = true;

        }
        if (beforeBook.getBookShelf() != null && beforeBook.getBookShelf().equals(book.getBookShelf())) {
            shelfFlag = true;

        }
        if (beforeBook.getFace() != null && beforeBook.getFace().equals(book.getFace())) {
            faceFlag = true;
        }
        if (beforeBook.getSeries() != null && beforeBook.getSeries().equals(book.getSeries())) {
            seriesFlag = true;
        }

        book.setIsOK(storeHouseFlag && rowFlag && shelfFlag && faceFlag && seriesFlag);
    }
}
